package com.caogt.user.userInfo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.caogt.user.userInfo.entity.UserInfo;
import com.caogt.user.userInfo.mapper.UserInfoMapper;
import com.caogt.user.userInfo.service.IUserInfoService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author caogt
 * @since 2019-06-15
 */
@Service
public class UserInfoServiceImpl implements IUserInfoService {

    @Autowired
    private UserInfoMapper userInfoMapper;

    @Override
    public UserInfo getById(Long id) {
        return userInfoMapper.selectById(id);
    }

    @Override
    public void save(UserInfo userInfo) {
        userInfoMapper.insert(userInfo);
    }

    @Override
    public boolean removeById(Long id) {
        return userInfoMapper.deleteById(id) == 1;
    }

    @Override
    public List<UserInfo> getUserByOrgId(Long orgId) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("org_id",orgId);
        return userInfoMapper.selectList(queryWrapper);
    }

    @Override
    public boolean validatePassword(String loginName, String password) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("login_name",loginName);
        queryWrapper.eq("password",password);
        List<UserInfo> userInfos = userInfoMapper.selectList(queryWrapper);
        if(userInfos == null || userInfos.size() == 0){
            return false;
        }else if(userInfos.size() > 1){
            throw new RuntimeException("数据异常");
        }
        return true;
    }

    @Override
    public List<UserInfo> query(String name) {
        QueryWrapper queryWrapper = new QueryWrapper();
        if(StringUtils.isNotBlank(name)){
            queryWrapper.eq("user_name",name);
        }
        return userInfoMapper.selectList(queryWrapper);
    }
}
